#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>

int g_count  = 0;

unsigned int g_common_addr = 0;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //lb_led.setLED(ui->lb_led, 1, 16);


    g_pUdpclient = new XUdp(6025);

    g_pUdpclient->CreateSocket();
    int ret = g_pUdpclient->Bind();

    if(ret == true)
    {

        g_pUdpclient->SetSendTimeout(5);
        g_pUdpclient->SetRecvTimeout(1);
    }

    thread = new QThread(this);
    moveThread = new NetRecv();
    moveThread->moveToThread(thread);
    thread->start();

    connect(this, &Widget::udp_recv, moveThread,&NetRecv::ReadData);
    connect(moveThread, &NetRecv::UpdateStatus, this, &Widget::on_update_status);
    g_IsRunningNet = false;

    //启动定时器    单位 毫秒
    timer_id = startTimer(1000);

    m_send_flag = false;


}

Widget::~Widget()
{
    g_pUdpclient->Close();
    delete g_pUdpclient;
    delete ui;
}


void Widget::on_btn_net_connect_clicked()
{
    QString ipstr = ui->cb_net_connect->currentText();
    QStringList list = ipstr.split(":");        //以冒号为标志进行分割
    g_pUdpclient->Send("Hello", 5,list[1].toLatin1().data(),list[2].toUShort());

    memset(m_ip,0,sizeof(m_ip));
    memcpy(m_ip,list[1].toLatin1().data(),list[1].toLatin1().size());
    m_port = list[2].toUShort();

    addLogMess("IP已生效", Qt::GlobalColor::darkGreen);
    g_IsRunningNet = true;
    emit udp_recv();

}

void Widget::on_update_status(int num)
{
    qDebug()<<"size_:"<<num;
    if(num == 0)
    {
        ui->btn_net_connect->setText("断开");
        on_btn_net_connect_clicked();
    }
    if((num > 0)&&(num<=8192))
    {
        g_mutex_update.lock();
        memcpy(m_recv_data,g_recv_buf,num);
        memcpy(&m_net_data,m_recv_data,num);
        g_mutex_update.unlock();

        if(m_net_data.cmd == 0x03)
        {
            QFile file(m_path_filename);
            if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) {
                qDebug() << "Failed to open the file.";
            }
            else
            {
                if(m_net_data.count == 1)
                    addLogMess("开始保存！", Qt::GlobalColor::darkGreen);

                if(m_net_data.count == 1024)
                    addLogMess("保存完成！", Qt::GlobalColor::darkGreen);

                // 创建数据流，用于写入二进制数据
                QDataStream out(&file);
                // 写入 char 数组
                out.writeRawData(m_net_data.buf, 4096);
                // 关闭文件
                file.close();
                g_count++;
                QString str_text = "数据计数：" + QString::number(g_count);

                addLogMess(str_text, Qt::GlobalColor::darkGreen);
                //qDebug() << "Binary data appended successfully.";
            }

        }
        if(m_net_data.cmd == 0x04)
        {
            if(m_net_data.addr == VERSION)
            {
                ui->lineEdit_2->setText("0x"+QString::number(m_net_data.value, 16));

            }

            if(m_net_data.addr == AD7134_R_VALUE)
            {
                ui->AD7134_value->setText("0x"+QString::number(m_net_data.value, 16));

            }
            if(m_net_data.addr == AD5755_VALUE)
            {
                ui->ed_AD5755_VALUE->setText("0x"+QString::number(m_net_data.value, 16));

            }
            if(m_net_data.addr == EEPROM_VALUE)
            {
                ui->ed_EEPROM_VALUE->setText("0x"+QString::number(m_net_data.value, 16));

            }
            if(m_net_data.addr == BISS_VALUE)
            {
                ui->ed_BISS_VALUE->setText("0x"+QString::number(m_net_data.value, 16));

            }
            if(m_net_data.addr == SIGNAL_0)
            {
                ui->ed_SIGNAL_0->setText("0x"+QString::number(m_net_data.value, 16));

            }
            if(m_net_data.addr == SIGNAL_1)
            {
                ui->ed_SIGNAL_1->setText("0x"+QString::number(m_net_data.value, 16));

            }
            if(m_net_data.addr == SIGNAL_2)
            {
                ui->ed_SIGNAL_2->setText("0x"+QString::number(m_net_data.value, 16));

            }
            if(m_net_data.addr == SIGNAL_3)
            {
                ui->ed_SIGNAL_3->setText("0x"+QString::number(m_net_data.value, 16));

            }
            if(m_net_data.addr == EC11_NUM)
            {
                ui->ed_EC11_NUM->setText("0x"+QString::number(m_net_data.value, 16));

            }
            if(m_net_data.addr == EC11_KEY)
            {
                ui->ed_EC11_KEY->setText("0x"+QString::number(m_net_data.value, 16));

            }
            if(m_net_data.addr == g_common_addr)
            {
                ui->ed_common_value->setText("0x"+QString::number(m_net_data.value, 16));
            }
        }
    }
}

void Widget::addLogMess(QString mes, Qt::GlobalColor color)
{
    ui->log_text->moveCursor(QTextCursor::End);
    mes.append("\n");
    ui->log_text->setTextColor(color);
    ui->log_text->insertPlainText(mes);
    QTextCursor _qc = ui->log_text->textCursor();
    _qc.movePosition(QTextCursor::End);
    ui->log_text->setTextCursor(_qc);
}


//void Widget::on_btn_collect_clicked()
//{

//    m_send_flag = true;
//    g_count = 0;

//}

void Widget::timerEvent(QTimerEvent * ev)
{

    if(ev->timerId() == timer_id)
    {
//        if(m_send_flag == true)
//        {
//            m_net_cmd.frame_head[0] = 0x55;
//            m_net_cmd.frame_head[1] = 0xaa;
//            m_net_cmd.device_id = 0x01;
//            m_net_cmd.cmd = 0x02;
//            m_net_cmd.frame_end[0] = 0xaa;
//            m_net_cmd.frame_end[1] = 0x55;

//            m_net_cmd.addr = 0x1000 / 4;
//            m_net_cmd.value = 0x00;
//            memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);


//            m_net_cmd.addr =(0x40/4);
//            m_net_cmd.value = ui->lineEdit_3->text().toUInt(nullptr, 16);
//            memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr =(0x44/4);
//            m_net_cmd.value = ui->lineEdit_4->text().toUInt(nullptr, 16);
//            memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);


//            m_net_cmd.addr =(0x48/4);
//            m_net_cmd.value = ui->lineEdit_5->text().toUInt(nullptr, 16);
//            memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr =(0x4c/4);
//            m_net_cmd.value = ui->lineEdit_6->text().toUInt(nullptr, 16);
//            memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr =(0x50/4);
//            m_net_cmd.value = ui->lineEdit_7->text().toUInt(nullptr, 16);
//            memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr =(0x54/4);
//            m_net_cmd.value = ui->lineEdit_8->text().toUInt(nullptr, 16);
//            memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr =(0xc0/4);
//            m_net_cmd.value = ui->lineEdit_9->text().toUInt(nullptr, 16);
//            memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr =(0xc4/4);
//            m_net_cmd.value = ui->lineEdit_10->text().toUInt(nullptr, 16);
//            memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr =(0xc8/4);
//            m_net_cmd.value = 0x01;
//            memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr = 0xCC / 4;
//            m_net_cmd.value = 0x00;
//            memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr = 0xD0 / 4;
//            m_net_cmd.value = 0x00;
//            memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr = 0x2C / 4;
//            m_net_cmd.value = 0x00;
//            memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr = 0x20000 / 4;
//            m_net_cmd.value = 0x01;
//            memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);

//            m_net_cmd.addr = 0x1000 / 4;
//            m_net_cmd.value = 0x01;
//            memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
//            g_pTcpClient->Send(m_send_data, sizeof(m_net_cmd));
//            QThread::msleep(50);


//            m_send_flag = false;

//        }



    }


}


void Widget::on_btn_version_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = VERSION;
    m_net_cmd.value = 0x00;
    memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);

}


void Widget::on_btn_AD7134_ENABLE_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = AD7134_ENABLE;
    m_net_cmd.value = ui->AD7134_ENABLE_value->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);

}

void Widget::on_btn_AD7134_W_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = AD7134_W;
    m_net_cmd.value = ui->ed_AD7134_W->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);

}

void Widget::on_btn_AD7134_R_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = AD7134_R;
    m_net_cmd.value = ui->ed_AD7134_R->text().toUInt(nullptr, 16);
    memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);

}
void Widget::on_btn_AD7134_R_2_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = AD7134_R_VALUE;
    m_net_cmd.value = ui->AD7134_value->text().toUInt(nullptr, 16);
    memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}


void Widget::on_btn_AD7134_CS_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = AD7134_CS;
    m_net_cmd.value = ui->ed_AD7134_CS->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}



void Widget::on_btn_ed_AD7134_Reset_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = AD7134_RESET;
    m_net_cmd.value = ui->ed_AD7134_Reset->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);

}

void Widget::on_btn_AD5755_ENABLE_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = AD5755_ENABLE;
    m_net_cmd.value = ui->ed_AD5755_ENABLE->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_AD5755_RW_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = AD5755_RW;
    m_net_cmd.value = ui->ed_AD5755_RW->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_AD5755_VALUE_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = AD5755_VALUE;
    m_net_cmd.value = ui->ed_AD5755_VALUE->text().toUInt(nullptr, 16);
    memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_AD5755_RESET_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = AD5755_RESET;
    m_net_cmd.value = ui->ed_AD5755_RESET->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_TMC5130_ENABLE_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = TMC5130_ENABLE;
    m_net_cmd.value = ui->ed_TMC5130_ENABLE->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_TMC5130_RW_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = TMC5130_RW;
    m_net_cmd.value = ui->ed_TMC5130_RW->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_TMC5130_W_VALUE_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = TMC5130_W_VALUE;
    m_net_cmd.value = ui->ed_TMC5130_W_VALUE->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_TMC5130_MODE_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = TMC5130_MODE;
    m_net_cmd.value = ui->ed_TMC5130_MODE->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_TMC5130_SPEED_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = TMC5130_SPEED;
    m_net_cmd.value = ui->ed_TMC5130_SPEED->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_TMC5130_ANGLE_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = TMC5130_ANGLE;
    m_net_cmd.value = ui->ed_TMC5130_ANGLE->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_TMC5130_MODE_EN_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = TMC5130_MODE_EN;
    m_net_cmd.value = ui->ed_TMC5130_MODE_EN->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_TMC5130_STOP_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = TMC5130_STOP;
    m_net_cmd.value = ui->ed_TMC5130_STOP->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_TMC5130_XUANZHUAN_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = TMC5130_XUANZHUAN;
    m_net_cmd.value = ui->ed_TMC5130_XUANZHUAN->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_pwm_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = PWM;
    m_net_cmd.value = ui->ed_PWM->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_EEPROM_EN_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = EEPROM_EN;
    m_net_cmd.value = ui->ed_EEPROM_EN->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_EEPROM_RW_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = EEPROM_RW;
    m_net_cmd.value = ui->ed_EEPROM_RW->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_EEPROM_VALUE_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = EEPROM_VALUE;
    m_net_cmd.value = ui->ed_EEPROM_VALUE->text().toUInt(nullptr, 16);
    memcpy(m_send_data, &m_net_cmd, sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}


void Widget::on_btn_BISS_EN_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = BISS_EN;
    m_net_cmd.value = ui->ed_BISS_EN->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_BISS_VALUE_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = BISS_VALUE;
    m_net_cmd.value = ui->ed_BISS_VALUE->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_SIGNAL_0_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = SIGNAL_0;
    m_net_cmd.value = ui->ed_SIGNAL_0->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_SIGNAL_1_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = SIGNAL_1;
    m_net_cmd.value = ui->ed_SIGNAL_1->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_SIGNAL_2_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = SIGNAL_2;
    m_net_cmd.value = ui->ed_SIGNAL_2->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_SIGNAL_3_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = SIGNAL_3;
    m_net_cmd.value = ui->ed_SIGNAL_3->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_EC11_NUM_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = EC11_NUM;
    m_net_cmd.value = ui->ed_EC11_NUM->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_EC11_KEY_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = EC11_KEY;
    m_net_cmd.value = ui->ed_EC11_KEY->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_common_set_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x02;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = ui->ed_common_addr->text().toUInt(nullptr, 16);;
    m_net_cmd.value = ui->ed_common_value->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);
}

void Widget::on_btn_common_read_clicked()
{
    m_net_cmd.frame_head[0] = 0x55;
    m_net_cmd.frame_head[1] = 0xaa;
    m_net_cmd.device_id = 0x01;
    m_net_cmd.cmd = 0x01;
    m_net_cmd.frame_end[0] = 0xaa;
    m_net_cmd.frame_end[1] = 0x55;

    m_net_cmd.addr = ui->ed_common_value->text().toUInt(nullptr, 16);
    g_common_addr = m_net_cmd.addr;
    m_net_cmd.value = ui->ed_common_value->text().toUInt(nullptr, 16);
    memcpy(m_send_data,&m_net_cmd,sizeof(m_net_cmd));
    g_pUdpclient->Send(m_send_data, sizeof(m_net_cmd),m_ip,m_port);

}
